package com.crm.sys.tab;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.google.common.base.Strings;
import com.crm.sys.model.Department;
import com.crm.sys.service.SysService;
/**
 *  部门标签
 * @author Administrator
 *
 */
@Repository
public class DepartmentTag extends RequestContextAwareTag {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4645294533418720134L;
    private String value; //传入的默认值
    private int type;  //类型  1 显示内容  2显示下拉框
    private Long did; //用户所在部门
	public void setValue(String value) {
		this.value = value;
	}
	public void setType(int type) {
		this.type = type;
	}
	public void setDid(Long did){
		this.did = did;
	}

	public DepartmentTag(){  
    }  
      
	@Override
	protected int doStartTagInternal() throws Exception {  
        JspWriter out = this.pageContext.getOut();  
          
        try {  
        	SysService sysService  = (SysService)this.getRequestContext().getWebApplicationContext().getBean("sysService");  
          	 
        	  try{
        		  String outString ="";
        		  	if(Strings.isNullOrEmpty(value)){
        		  		value="-1";
        		  	}
        		    switch (type) {
					case 1:
						Department   dep = sysService.getDepartmentById(Long.parseLong(value));
						if (dep != null) {
							outString =dep.getName();
						} else {
							outString ="";
						}
						break; 
					case 2:
						StringBuffer  sb;
						if (did != null && did.SIZE > 0) {
							sb = getDeptHtml(sysService.getAllDeptmentByPid(did),new StringBuffer(),"");
						} else {
							sb = getDeptHtml(sysService.getDepartmentAll(),new StringBuffer(),"");
						}
						outString = sb.toString();
						break;
					case 3:
						StringBuffer  sb1 =  getDeptHtml1(sysService.getDepartmentAll(),new StringBuffer(),"");
						outString = sb1.toString();
						break;
					}
        		 out.print(outString);  
		       	}catch(Exception e1){
		       		e1.printStackTrace();
		       	 out.print("");  
		       	}
           
        } catch (IOException e) {  
            //e.printStackTrace();  
        }  
          
      
        return EVAL_BODY_INCLUDE;  
    }  
	/**
	 *   取得部门下拉框html
	 * @param dept
	 * @param s   拼接字符串
	 * @param blankStr  空白字符串
	 * @return
	 */
	private StringBuffer getDeptHtml(List<Department> dept,StringBuffer s,String blankStr){
		
		if (dept != null) {
			for(Department d :dept){
				  s.append("<option value='").append(d.getId()).append("' ").append(d.getId()==Integer.parseInt(value)?"selected >":">").append(blankStr+d.getName()).append("</option>");
				  s= getDeptHtml(d.getDepList(), s, blankStr+"&nbsp;&nbsp;");
			 }
		}
		return s;
	}
	/**
	 * 取得部门下拉框html加0
	 * */
	private StringBuffer getDeptHtml1(List<Department> dept,StringBuffer s,String blankStr){
		 for(Department d :dept){
			  s.append("<option value='").append(d.getId()).append("' ").append(d.getId()==Integer.parseInt(value)?"selected >":">").append(blankStr+d.getName()).append("</option>");
			  s= getDeptHtml(d.getDepList(), s, blankStr+"&nbsp;&nbsp;");
		 }
		return s;
	}
	
 
 
    public int doAfterBody() throws JspException{  
       // System.out.println("处理标签体（after body）....");  
        return SKIP_BODY;  
    }  
    public int doEndTag() throws JspException{  
      //  System.out.println("对象正在处理结束标记.....");  
        return EVAL_PAGE;  
    }  
}
